﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.CreateWPOnRobotTCPF.CApCreateWpOnTCPFMgr
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using DnProcessSimulateCommands.CreateWeldPoint;
using System.Collections.ObjectModel;
using System.Reflection;
using System.Resources;
using Tecnomatix.Engineering;
using Tecnomatix.Engineering.DataTypes;


namespace DnProcessSimulateCommands.CreateWPOnRobotTCPF;

internal class CApCreateWpOnTCPFMgr
{
  public void CreateWpOnRobotTCPF()
  {
    TxObjectList filteredItems1 = TxApplication.ActiveSelection.GetFilteredItems((ITxTypeFilter) new TxTypeFilter(typeof (TxRobot)));
    TxApplication.ActiveUndoManager.StartTransaction();
    foreach (TxRobot robot in (Collection<ITxObject>) filteredItems1)
      this.CreateWpOnCurrentLocationTCPF(robot);
    TxObjectList filteredItems2 = TxApplication.ActiveSelection.GetFilteredItems((ITxTypeFilter) new TxTypeFilter(typeof (TxGun)));
    TxObjectList filteredItems3 = TxApplication.ActiveSelection.GetFilteredItems((ITxTypeFilter) new TxTypeFilter(typeof (TxServoGun)));
    TxObjectList filteredItems4 = TxApplication.ActiveSelection.GetFilteredItems((ITxTypeFilter) new TxTypeFilter(typeof (TxPneumaticServoGun)));
    if (((Collection<ITxObject>) filteredItems2).Count > 0)
    {
      foreach (TxGun txGun in (Collection<ITxObject>) filteredItems2)
        this.CreateWpOnCurrentLocationTCP(txGun.TCPF);
    }
    else if (((Collection<ITxObject>) filteredItems3).Count > 0)
    {
      foreach (TxServoGun txServoGun in (Collection<ITxObject>) filteredItems3)
        this.CreateWpOnCurrentLocationTCP(txServoGun.TCPF);
    }
    else if (((Collection<ITxObject>) filteredItems4).Count > 0)
    {
      foreach (TxPneumaticServoGun pneumaticServoGun in (Collection<ITxObject>) filteredItems4)
        this.CreateWpOnCurrentLocationTCP(pneumaticServoGun.TCPF);
    }
    TxApplication.ActiveUndoManager.EndTransaction();
  }

  public void CreateWpOnRobotTCPF(
    TxRobot robot,
    string wpName,
    TxCompoundPart weldContainer,
    ITxWeldOperation weldOperation)
  {
    if (!TxRobot.op_Inequality(robot, (ITxObject) null) || !TxFrame.op_Inequality(robot.TCPF, (ITxObject) null) || wpName.Length <= 0 || (CApCreateWpTCLogic.IsWpConfiguredAsPrivate() ? 1 : (TxCompoundPart.op_Inequality(weldContainer, (ITxObject) null) ? 1 : 0)) == 0 || weldOperation == null)
      return;
    this.CreateWeldPoint(robot.TCPF.AbsoluteLocation, wpName, weldContainer, weldOperation);
  }

  public void CreateWpOnRobotTCPF(
    ITxTool tool,
    string wpName,
    TxCompoundPart weldContainer,
    ITxWeldOperation weldOperation)
  {
    if (tool == null || !TxFrame.op_Inequality(tool.TCPF, (ITxObject) null) || wpName.Length <= 0 || (CApCreateWpTCLogic.IsWpConfiguredAsPrivate() ? 1 : (TxCompoundPart.op_Inequality(weldContainer, (ITxObject) null) ? 1 : 0)) == 0 || weldOperation == null)
      return;
    this.CreateWeldPoint(tool.TCPF.AbsoluteLocation, wpName, weldContainer, weldOperation);
  }

  private void CreateWpOnCurrentLocationTCPF(TxRobot robot)
  {
    TxFrame tcpf = robot.TCPF;
    if (!TxFrame.op_Inequality(tcpf, (ITxObject) null))
      return;
    this.CreateWeldPoint(tcpf.AbsoluteLocation);
  }

  private void CreateWpOnCurrentLocationTCP(TxFrame frame)
  {
    if (!TxFrame.op_Inequality(frame, (ITxObject) null))
      return;
    this.CreateWeldPoint(frame.AbsoluteLocation);
  }

  private void CreateWeldPoint(TxTransformation absoluteLocation)
  {
    Assembly assembly = this.GetType().Assembly;
    ResourceManager resourceManager = new ResourceManager(assembly.GetName().Name + ".StringTable", assembly);
    TxPhysicalRoot physicalRoot = TxApplication.ActiveDocument.PhysicalRoot;
    TxOperationRoot operationRoot = TxApplication.ActiveDocument.OperationRoot;
    string str = resourceManager.GetString("WELD_NEW_WELD_POINT_PREFIX");
    string uniqueDisplayName = physicalRoot.GetUniqueDisplayName(str);
    TxWeldPointCreationData pointCreationData = new TxWeldPointCreationData(uniqueDisplayName, absoluteLocation.Translation);
    double num = pointCreationData.CoolDownTime + pointCreationData.MotionTime + pointCreationData.WeldTime;
    TxWeldLocationOperationCreationData operationCreationData = new TxWeldLocationOperationCreationData(uniqueDisplayName, "", num, (ITxWeldPointCreationData) pointCreationData);
    TxWeldLocationOperation locationOperation = operationRoot.CreateWeldLocationOperation(operationCreationData);
    if (!TxWeldLocationOperation.op_Inequality(locationOperation, (ITxObject) null))
      return;
    locationOperation.WeldPoint.AbsoluteLocation = absoluteLocation;
  }

  private void CreateWeldPoint(
    TxTransformation absoluteLocation,
    string wpName,
    TxCompoundPart weldContainer,
    ITxWeldOperation weldOperation)
  {
    ITxWeldPointCreationData pointCreationData = TxMfgCreationDataFactory.CreateWeldPointCreationData(wpName, absoluteLocation.Translation);
    if (TxCompoundPart.op_Inequality(weldContainer, (ITxObject) null))
      pointCreationData = (ITxWeldPointCreationData) TxMfgCreationDataFactory.CreateTcExposedWeldPointCreationData(wpName, absoluteLocation.Translation, TxMfgCreationDataFactory.CreateTcExposedMfgData(weldContainer));
    double num = pointCreationData.CoolDownTime + pointCreationData.MotionTime + pointCreationData.WeldTime;
    TxWeldLocationOperationCreationData operationCreationData = new TxWeldLocationOperationCreationData(wpName, "", num, pointCreationData);
    TxWeldLocationOperation locationOperation = (TxWeldLocationOperation) null;
    if (((ITxWeldLocationOperationCreation) weldOperation).CanCreateWeldLocationOperation(operationCreationData))
      locationOperation = ((ITxWeldLocationOperationCreation) weldOperation).CreateWeldLocationOperation(operationCreationData);
    if (!TxWeldLocationOperation.op_Inequality(locationOperation, (ITxObject) null))
      return;
    locationOperation.WeldPoint.AbsoluteLocation = absoluteLocation;
  }
}
